﻿using Microsoft.AspNetCore.Authorization;

namespace Simple.WebApi.Controllers.System
{
    /// <summary>
    /// 课程分类管理
    /// </summary>
    [Route("api/Course/[action]")]
    [ApiController]
    [Authorize]
    public class CourseController : ControllerBase
    {
        private readonly CourseService _courseService;

        public CourseController(CourseService courseService)
        {
            _courseService = courseService;
        }

        /// <summary>
        /// 课程列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> List()
        {
            List<CourseModel> data = await _courseService.GetAsync();
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 课程查询
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> Page([FromQuery] CoursePageInputModel model)
        {
            PageResultModel<CourseModel> data = await _courseService.GetPageAsync(model);
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 课程增加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Add([FromForm] CourseInputModel model)
        {
            await _courseService.AddAsync(model);
            return AppResult.Status200OK("新增成功");
        }

        /// <summary>
        /// 课程编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Edit([FromForm] CourseInputModel model)
        {
            await _courseService.UpdateAsync(model);
            return AppResult.Status200OK("更新成功");
        }

        /// <summary>
        /// 课程删除
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Delete([FromBody] List<IdInputModel> models)
        {
            await _courseService.DeleteAsync(models.Select(m => m.Id));
            return AppResult.Status200OK("删除成功");
        }

        /// <summary>
        /// 课程启用/禁用
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> ChangeEnable([FromBody] ChangeEnableDto models)
        {
            await _courseService.ChangeEnable(models);
            return AppResult.Status200OK($"{(models.IsEnable ? "启用" : "禁用")}成功");
        }

        /// <summary>
        /// 播放
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Play([FromBody] IdInputModel input)
        {
            await _courseService.Play(input.Id);
            return AppResult.Status200OK("开始播放");
        }

        #region 移动端
        /// <summary>
        /// 课程查询(移动端)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> MobilePage([FromQuery] MobileCoursePageInputModel model)
        {
            PageResultModel<MobileCourseModel> data = await _courseService.GetMobilePageAsync(model);
            return AppResult.Status200OK(data: data);
        }
        #endregion
    }
}
